//
//  CXHToBidInterstitialAd.m
//  CXHAdSDK-ToBid
//
//  Created by 陈坤 on 2025/4/7.
//

#import "CXHToBidInterstitialAd.h"
#import <WindMillSDK/WindMillSDK.h>
#import <WindFoundation/WindFoundation.h>
#import <CXHAdSDK-Core/CXHAdSDKInterstitialAd.h>

@interface CXHToBidInterstitialAd ()<CXHAdSDKInterstitialAdDelegate>

@property (nonatomic, weak) id<AWMCustomInterstitialAdapterBridge> bridge;
@property (nonatomic, weak) id<AWMCustomInterstitialAdapter> adapter;
@property (nonatomic, strong) CXHAdSDKInterstitialAd *interstitialAd;
@property (nonatomic, assign) BOOL isReady;

@end

@implementation CXHToBidInterstitialAd

- (instancetype)initWithBridge:(id<AWMCustomAdapterBridge>)bridge adapter:(id<AWMCustomAdapter>)adapter {
    self = [super init];
    if (self) {
        _bridge = (id<AWMCustomInterstitialAdapterBridge>)bridge;
        _adapter = (id<AWMCustomInterstitialAdapter>)adapter;
    }
    return self;
}
- (BOOL)mediatedAdStatus {
    return self.isReady;
}
- (void)loadAdWithPlacementId:(NSString *)placementId
                    parameter:(AWMParameter *)parameter {
    self.interstitialAd = [[CXHAdSDKInterstitialAd alloc] init];
    self.interstitialAd.delegate = self;
    self.interstitialAd.posId = placementId;
    [self.interstitialAd loadAdData];
}
- (BOOL)showAdFromRootViewController:(UIViewController *)viewController parameter:(AWMParameter *)parameter {
    self.interstitialAd.controller = viewController;
    [self.interstitialAd show];
    return YES;
}
- (void)didReceiveBidResult:(AWMMediaBidResult *)result {
    if (result.win) {
        [self.interstitialAd sendWinNotice:result.winnerPrice];
        return;
    }
    // todo: 当胜出时，winnerPrice有值，其它情况不回传胜出价
    [self.interstitialAd sendLossNotice:result.winnerPrice];
}

#pragma mark - CXHAdSDKInterstitialAdDelegate
/**
 CXHAdSDKIntertitialAd请求成功回调
 
 @param interstitialAd 插屏广告实例对象
*/
- (void)CXH_interstitialAdSuccedToLoad:(CXHAdSDKInterstitialAd *)interstitialAd{
    WindmillLogDebug(@"[cxh]", @"%@", NSStringFromSelector(_cmd));
    NSString *price = [NSString stringWithFormat:@"%ld", [self.interstitialAd getBidPrice]];
    [self.bridge interstitialAd:self.adapter didAdServerResponseWithExt:@{
        AWMMediaAdLoadingExtECPM: price
    }];
    self.isReady = YES;
    [self.bridge interstitialAdDidLoad:self.adapter];
}

/**
 CXHAdSDKIntertitialAd请求失败回调

 @param interstitialAd 插屏广告实例对象
 @param error 失败原因
*/
- (void)CXH_interstitialAdFailedToLoad:(CXHAdSDKInterstitialAd *)interstitialAd error:(NSError *)error{
    WindmillLogDebug(@"[cxh]", @"%@", NSStringFromSelector(_cmd));
    self.isReady = NO;
    [self.bridge interstitialAd:self.adapter didLoadFailWithError:error ext:nil];
}

/**
 CXHAdSDKIntertitialAd展示在屏幕内回调

 @param interstitialAd 插屏广告实例对象
*/
- (void)CXH_interstitialAdDidPresent:(CXHAdSDKInterstitialAd *)interstitialAd{
    
}

/**
 CXHAdSDKIntertitialAd展示在屏幕内失败回调

 @param interstitialAd 插屏广告实例对象
*/
- (void)CXH_interstitialAdFailedToPresent:(CXHAdSDKInterstitialAd *)interstitialAd error:(NSError *)error{
    WindmillLogDebug(@"[cxh]", @"%@", NSStringFromSelector(_cmd));
    self.isReady = NO;
    [self.bridge interstitialAdDidShowFailed:self.adapter error:error];
}

/**
 CXHAdSDKIntertitialAd点击回调

 @param interstitialAd 插屏广告实例对象
 @param loadingPageURL 广告落地页地址，当渠道为bwt，并且customLoadingPage为YES时有值
*/
- (void)CXH_interstitialAdDidClick:(CXHAdSDKInterstitialAd *)interstitialAd loadingPageURL:(NSString *)loadingPageURL{
    WindmillLogDebug(@"[cxh]", @"%@", NSStringFromSelector(_cmd));
    [self.bridge interstitialAdDidClick:self.adapter];
}

/**
 CXHAdSDKIntertitialAd关闭回调

 @param interstitialAd 插屏广告实例对象
*/
- (void)CXH_interstitialAdDidClose:(CXHAdSDKInterstitialAd *)interstitialAd{
    WindmillLogDebug(@"[cxh]", @"%@", NSStringFromSelector(_cmd));
    [self.bridge interstitialAdDidClose:self.adapter];
}

/**
 CXHAdSDKIntertitialAd展示回调
 
 @param interstitialAd 广告实例
 */
- (void)CXH_interstitialAdExposure:(CXHAdSDKInterstitialAd *)interstitialAd{
    WindmillLogDebug(@"[cxh]", @"%@", NSStringFromSelector(_cmd));
    self.isReady = NO;
    [self.bridge interstitialAdDidVisible:self.adapter];
}

/**
 CXHAdSDKIntertitialAd展示上报回调
 
 @param interstitialAd 广告实例
 */
- (void)CXH_interstitialAdExposureReport:(CXHAdSDKInterstitialAd *)interstitialAd{
    
}


/**
 CXHAdSDKIntertitialAd点击上报回调

 @param interstitialAd 插屏广告实例对象
*/
- (void)CXH_interstitialAdDidClickReport:(CXHAdSDKInterstitialAd *)interstitialAd{
    
}

/**
 CXHAdSDKIntertitialAd关闭落地页回调
 
 @param interstitialAd 广告实例
 */
- (void)CXH_interstitialAdCloseLandingPage:(CXHAdSDKInterstitialAd *)interstitialAd{
    
}

@end
